package com.judian.smart.util;

import android.os.AsyncTask;

import com.judian.smart.constant.Constants;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class AiuiDataServiceUtils {
    public static AiuiDataServiceUtils instance;

    static {
        if (instance == null) {
            instance = new AiuiDataServiceUtils();
        }
    }

    static String endPoint = Constants.CHNIES + "Service/AiuiDataService.asmx";
    static String NAMESPACE = "http://tempuri.org/";

    public String insertAiuiData(String DataName, String DataType,
                                 String DataText, String MasterID, String ToID)
            throws Exception {
        String methodName = "insertAiuiData";
        String soapAction = NAMESPACE + methodName;
        SoapObject rpc = new SoapObject(NAMESPACE, methodName);
        rpc.addProperty("DataName", DataName);
        rpc.addProperty("DataType", DataType);
        rpc.addProperty("DataText", DataText);
        rpc.addProperty("MasterID", MasterID);
        rpc.addProperty("ToID", ToID);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = rpc;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(rpc);
        HttpTransportSE transport = new HttpTransportSE(endPoint);
        transport.call(soapAction, envelope);
        SoapObject object = (SoapObject) envelope.bodyIn;
        return object.getProperty(0).toString();
    }

    public void postString1(String DataName, String DataType,
                            String DataText, String MasterID, String ToID) {

        new AsynDataJsonArrayTask().execute(DataName, DataType, DataText, MasterID, ToID);
    }

    private class AsynDataJsonArrayTask extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            String json = "";
            try {
                json = insertAiuiData(params[0], params[1], params[2], params[3], params[4]);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return json;
        }

        @Override
        protected void onPostExecute(String resString) {
            // TODO Auto-generated method stub
            super.onPostExecute(resString);

        }
    }

}
